Skip to content

fix(confidence): a probability too wide for a double is no number, not a crash - #1405

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:confidence-jev-overflow
Oct 28, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:confidence-jev-overflow

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

jev_confidence's malformed-document arm catches OverflowError as well as TypeError and ValueError. Three types in one except, no new message, no behaviour change for any input that returns a number today.

Why

laya/confidence.py:48-51 states the contract: "a malformed document -- a probability that does not read as a number, a boolean, a non-finite or a negative value -- gives None rather than a guess, so a caller filtering on this number never filters on a made-up one." The coercion at :62-68 only caught two of the three ways that conversion fails:

    except (TypeError, ValueError):
        return None

JSON has no double limit. A model or a client that emits a 400-digit integer literal parses through json.loads into a Python int, and float() on that raises OverflowError, which walks straight out of jev_confidence -- past the None the docstring promises and past the caller's if jev_confidence(answer) < floor filter. Both arms of the try leak it:

choice, probabilities {"a": <400 nines>}  -> OverflowError: int too large to convert to float
score, probabilities {"0": <400 nines>}   -> OverflowError: int too large to convert to float
score, probabilities {inf: 0.5, 2: 0.5}   -> OverflowError: cannot convert float infinity to integer   (from `sorted(..., key=int)`)
{"a": "abc"}                              -> None   (already refused)
{"a": inf}                                -> None   (already refused)

The third row is the same omission on the level keys rather than the values, which is why it is in the suite too. The module family already treats this exception as part of "not a number": calibrate.py:780 catches (TypeError, ValueError, OverflowError) with the comment "Overflow: JSON's 1e999 is inf", calibrate.py:80 catches the same three, and hooks.py:275 was fixed for exactly this pair of leaks.

How it was verified

LAYA_WT=$PWD .venv/bin/python -B tests/test_confidence.py          # 266 passed, 0 failed
LAYA_WT=$PWD .venv/bin/python -B tests/test_confidence.py          # same suite on pristine b9fe351: 263 passed
ruff check laya/confidence.py tests/test_confidence.py --select=E9,F63,F7,F82,F401,F811 --line-length=120

The three added rows are ("overflowing-integer-probability", ...), ("json-integer-too-wide-for-a-double", ...) -- built with json.loads so the case is the real remote-body route, not a hand-written Python literal -- and ("overflowing-score-level", ...) in the existing jev_confidence/none-for-%s table.

Mutation arm, the except tuple reverted to two types with the tests kept: the suite stops on the leak it exists to catch, naming it:

  File ".../tests/test_confidence.py", line 2479, in <module>
    check("jev_confidence/none-for-%s" % _name, jev_confidence(_answer), None)
OverflowError: int too large to convert to float

For the ports: laya-rust/tools/confidence_cases.py::_jev_corpus is the pinned jev surface (copied to .NET), so I ran every one of its cases through jev_confidence in this branch and in a pristine b9fe351 worktree and compared the results as one blob -- 96 cases, digest 7c10e13b0f4a69c7b008 on both, so no corpus output moves: this only turns a crash into the None the corpus already records for the neighbouring shapes. laya-java/scripts/gen_fixtures.py --check reports the same two families as a base worktree (lang_detect.json, python_json.json -- the Linux/macOS regeneration noted at gen_fixtures.py:1922), and neither family calls jev_confidence.

Checklist

  • Focused on one change (split unrelated work into another PR)
  • Rebased on the latest main
  • Tests pass locally
  • Docs or examples updated when the public API changed

…r, not as a crash

jev_confidence caught TypeError and ValueError but not OverflowError, so a
400-digit integer probability from JSON escaped a function whose docstring
promises no number rather than a made-up one.
@NandhaKishorM
NandhaKishorM merged commit e3d24ce into NandhaKishorM:main Oct 28, 2026
52 checks passed
NandhaKishorM added a commit that referenced this pull request Oct 28, 2026
24 PRs. Vietnamese kept off the English checkpoint (#1392), deciding-window
attribution preserved through end hooks (#1369), RemoteRouter identifying
itself with a configurable User-Agent (#1396), ten more inputs refused where
they enter (#1372, #1394, #1397, #1398, #1400, #1401, #1402, #1403, #1404,
#1405, #1406), per-language temperatures and per-call budgets in .NET and
Rust (#1370, #1371), .NET calibration files (#1384), the Go router lifecycle,
SHA-256 pins and routed inference (#1374, #1379, #1395), a C++ foundation
(#1399, #1407), and parity-lane sharding that replaces the budget headroom
added for 0.4.16 (#1373, #1375).

Verified before tagging: all 92 suites pass, 640 tests, up from 612. ruff
clean on both lists. laya-ts 1251 tests across 44 files, tsc --noEmit clean.
Every generated-artifact gate clean on CPython 3.12 / unicodedata 15.0.0:
Java 20 families match laya (9 unverified), UnicodeTables matches CPython
15.0.0, LanguageTables matches laya/lang.py, three Go tables_gen.go match,
both laya-cpp .inc files match. Version agrees across pyproject.toml,
laya.__version__ and nix/package.nix. README carries 0.4.17 only.

Security audit of the merge diff, 362766 added lines over 480 files: zero
obfuscation, zero secrets, zero boundary hits, zero binaries, no mode or
symlink changes, 20 of 20 actions SHA-pinned including the new cpp.yml, npm
install hooks unchanged, and no third-party host in any added line
(example.com 90, github.com 2, download.pytorch.org 1, docs.rs 1). The 12
dynamic-exec hits are the C++ test harness invoking its own checker through
sys.executable, the Go sample generator reading laya's own example by AST at
development time, and one comment in rust.yml.

Held: #1364 (unchanged, it moves a cross-port contract), #1381 (a #[doc(hidden)]
but pub Rust hook that would ship as public API able to replace the forward
pass), #1386 (route_batch switched from self.route to self._route, silently
bypassing subclass overrides the docstring promises), and nine stacked port
PRs that conflict with each other and need linearizing (#1377, #1378, #1380,
#1382, #1383, #1385, #1387, #1388, #1389, #1390).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged. Treating OverflowError as the same "does not read as a number" is correct: float() and int() refuse both a value that is not a number and one too wide to become one, and a function whose docstring promises None rather than a made-up number should not let the second case out as a crash.

The test cases are the strongest part. _json.loads with 400 nines is the realistic path, because JSON's grammar has no double limit, so a body like that arrives as a Python int and nothing upstream has a reason to reject it. The infinite score level is the matching case for int() rather than float(), which is easy to overlook since both live under the same except.

One small note, not a change request for this PR. The new comment cites calibrate.py:780, and line numbers drift as soon as anything above them moves. Citing the function or symbol survives edits. I have the same correction outstanding against a comment of my own in confidence.py, so this is a house habit worth breaking rather than anything specific to your change.

Shipped in 0.4.17.

NandhaKishorM added a commit that referenced this pull request Oct 28, 2026
…wError twin

#1405's comment pointed at `calibrate.py:780`, which drifts the moment anything
above it moves. The guard it means is the `version` field's
`except (TypeError, ValueError, OverflowError)` inside
`apply_calibration_payload`, so name that instead.

Comment only, no behaviour change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants